<template>
  <el-button type="primary" plain @click="dialogFormVisible = true">添加商品</el-button>
    <el-table :data="products" style="width: 100%">
      <el-table-column label="编号" prop="id" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="价格" prop="price" />
      <el-table-column label="型号" prop="type" />
      <el-table-column align="right">
        <template #header>
          <el-input v-model="keyword" size="small" placeholder="搜索商品"
                    @change="loadProduct(current)"/>
        </template>
        <template #default="scope">
          <el-button size="small" @click="editProduct(scope.row)">
            更新
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="deleteProduct(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  分页-->
    <el-pagination background layout="prev, pager, next" :total="total"
                   :page-size="pageSize" @current-change="loadProduct"/>
    <!--  对话框-->
    <el-dialog v-model="dialogFormVisible" title="商品信息" width="500"
               @close="product = {}">
      <el-form :model="product">
        <el-form-item label="名称" >
          <el-input v-model="product.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="价格" >
          <el-input v-model="product.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="型号" >
          <el-input v-model="product.type" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="saveProduct">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
</template>
<script>
import {ElMessage,ElMessageBox} from "element-plus";
export default {
  name: "ProductView",
  data(){
    return {
      products: [],//商品集合
      keyword: '', //搜索姓名
      current: 1, //当前页
      pageSize: 10, //页大小
      total: 10,   //总数
      dialogFormVisible: false, //对话框是否显示
      product: {},  //商品信息
    }
  },
  methods:{
    loadProduct(current){
      this.current = current
      this.$axios.get('/products-page?current=' + this.current + "&pageSize=" +
          this.pageSize +
          "&name=" + this.keyword)
          .then(result => {
            console.log('product',result.data)
            if(result.data){
              this.products = result.data.data;
              this.total = result.data.total;
            }
          })
    },
    //编辑商品
    editProduct(stu){
      //显示对话框
      this.dialogFormVisible = true;
      //显示商品信息  stringify object --> string  parse string ---> object
      this.product = JSON.parse(JSON.stringify(stu));
    },
    //保存商品
    saveProduct(){
      //通过id是否存在来判断是做添加还是更新
      if(this.product.id){
        //调用后台的更新商品接口
        this.$axios.put('/product',this.product)
            .then(result => {
              console.log('update',result.data)
              if(result.data){
                ElMessage.success('更新成功')
                //更新商品列表
                this.loadProduct(this.current)
              }
            })
      }else{
        //调用后台的添加商品接口
        this.$axios.post('/product',this.product)
            .then(result => {
              console.log('add',result.data)
              if(result.data){
                ElMessage.success('添加成功')
                //更新商品列表
                this.loadProduct(this.current)
              }
            })
      }
      this.dialogFormVisible = false
    },
    //删除商品
    deleteProduct(id){
      ElMessageBox.confirm(
          '是否确认删除?',
          'Warning',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        //调用后台的删除功能
        this.$axios.delete('/product/' + id)
            .then(result => {
              if(result.data){
                this.loadProduct(this.current)
              }
            })
      })
          .catch(() => {
          })
    }
  },
  mounted() {
    //默认加载第一页
    this.loadProduct(1)
  }
}
</script>
<style scoped>
</style>